<?php
/**
 * （logic）日销量统计报表-SKU (erp-FBASKU库存)
 * sv:fab-stock
 */

namespace App\Logic\SalesReport;

use App\Models\Basics\SysPlat;
use App\Models\SalesReport\DailySv;
use App\Models\SalesReport\FBAInventory;
use Illuminate\Support\Facades\DB;

class DailySalesVolumeUp
{
    /**
     * 日销量统计报表-更新FBA库存
     */
    public static function exe(){
        $platId = SysPlat::PLAT_AMAZON; //amazon
        DailySv::query()
            ->orderBy('id')
            ->chunk(800,function ($re) use($platId){
                DB::beginTransaction();
                try{
                    foreach($re as $_re){
                        if (intval($_re->plat_id) == $platId){
                            //fba库存
                            $inventory = FBAInventory::_getInventoryFind($_re->country_id,$_re->psku_id);
                            if($inventory){
                                $instant = $inventory->quantity ?: 0;
                                $transit = $inventory->inbound ?: 0;
                            }else{
                                $instant = 0;
                                $transit = 0;
                            }
                            //更新日销量fba库存
                            $_re->fba_instant_num = $instant;
                            $_re->fba_transit_num = $transit;
                            $_re->save();
                        }
                    }
                    DB::commit();
                }catch (\Exception $e){
                    DB::rollBack();
                    report($e);
                }
            });
    }
}
